#!/bin/bash -
#=======================================================================
#
#          FILE: extract.sh
#         USAGE: ./extract.sh <file>
#   DESCRIPTION: Extract archive file (support multi extensions)
#  REQUIREMENTS: tar and friends
#          BUGS: N/A
#         NOTES: Based on: http://mostlycli.blogspot.com/
#                2010/03/my-bashrc-file-part-3-making-life.html
#        AUTHOR: Ali Ayoub
#         EMAIL: ali@mellanox.com
#       COMPANY: N/A
#       CREATED: 09.17.2010-15:36:01
#      REVISION: 1.0
#=======================================================================

if [ ! $# == 1 ]; then
	echo "$0 <archive>"
	exit
fi

target=`basename $1`
PID=$$

set -x
if [ -f $1 ] ; then
        case $1 in
                *.tar.bz2)   tar xvjf $1        ;;
                *.tar.gz)    tar xvzf $1     ;;
                *.bz2)       bunzip2 $1       ;;
                *.rar)       unrar x $1     ;;
                *.gz)        gunzip $1     ;;
                *.tar)       tar xvf $1        ;;
                *.tbz2)      tar xvjf $1      ;;
                *.tgz)       tar xvzf $1       ;;
                *.zip)       unzip $1     ;;
                *.Z)         uncompress $1  ;;
                *.7z)        7z x $1    ;;
		*.cgz)       zcat $1 | cpio -i -d ;;
		*.rpm)       rpm2cpio $1 | cpio -idmv &> /dev/null ;;
		*.img)       gunzip < $1  | cpio  -i --make-directories ;;
		*.vib)       ar -x $1 && tar xvzf data.tar.gz ;;
		*.iso)       dst=$target.$PID.iso.dir && mkdir -p $dst && mount -o ro,loop $1 $dst && echo Use $dst, umount when done ;;
        esac
else
        echo "'$1' is not a valid file"
fi


